
// global variable holder
// so i won't pollute the browser/page namespace'
var game={};
game.debug=0;

function initWorld(){
    initGame();
    if(headless)
        return;
    if(game.bgsound)
        game.sound.playBGSound();
    if(game.debug==1){
        game.debugMessage = '';
        game.debugConsole = new Element('div',{'class': 'debug'});
        game.debugConsole.injectInside(document.body);
        game.appendLog = function(mesaj){
            game.debugMessage+='<br>'+mesaj;
        };
        game.writeLog=function(){
            this.debugConsole.set('html',this.debugMessage);
            this.debugMessage = '';
        };
        game.world.HTMLIncarnation.addEvent('click',function(e){
                                          var zone=util.getZoneAtPoint(e.event.clientX-150,e.event.clientY-20);
                                          game.clickedZone = zone;
                                          });
    }
}

function startGame() {
    game.sound = new DynaSound('DynaSound.swf');
    if(server){
        initWorld();
        return;
    }
    var introDiv = new Element('div',{'class': 'intro'});
    introDiv.set('html','<br>JS BOMBERMAN<BR>BACK IN GLORY<br>Arrows to move the hero and SPACE to launch a bomb.<br>P p ESC to pause<br><br>');
    var startButton = new Element('a',{'href': '#top','events': {'click': function(){
                                                      //loadImages();
                                                      var introEffect = new Fx.Tween(this,{'duration': 500});
                                                      introEffect.addEvent('onComplete',function(){this.destroy();initWorld();}.bind(this));
                                                      introEffect.start('opacity',1,0);
                                                    }.bind(introDiv)}});
    startButton.set('text','START');
    var startDebugButton = new Element('a',{'href': '#top','events': {'click': function(){
                                                      //loadImages();
                                                      var introEffect = new Fx.Tween(this,{'duration': 500});
                                                      introEffect.addEvent('onComplete',function(){game.debug=1;this.destroy();initWorld();}.bind(this));
                                                      introEffect.start('opacity',1,0);
                                                    }.bind(introDiv)}});
    startButton.set('text','START');
    startButton.inject(introDiv);
    var br=new Element('br');
    br.inject(introDiv);
    br.clone(false).inject(introDiv);
    startDebugButton.set('text','START-DEBUGMODE');
    startDebugButton.inject(introDiv);
    $('soundLevelIndicator').setOpacity(0.5);
    game.soundLevelIndicator = document.newTextNode('volume: 100%');
    $('soundLevelContainer').insertBefore(game.soundLevelIndicator,$('soundLevelIndicator'));
    $('soundLevelContainer').addEvent('click',function(e){
        var evt = new Event(e);
        evt.stopPropagation();
        var coords=$('soundLevelContainer').getCoordinates();
        var xEvent = evt.client.x;
        var volume = xEvent - coords.left;
        $('soundLevelIndicator').setStyle('width',volume);
        volume = parseInt(100*(volume/coords.width));
        if(Browser.Engine.trident)
            game.soundLevelIndicator.data='volume: '+volume+'%';
        else
            game.soundLevelIndicator.textContent='volume: '+volume+'%';
        game.sound.setVolume(volume);
    });

    introDiv.inject($('viewport'));
    $('bgsoundcheckbox').addEvent('click',function(){
    game.bgsound=game.bgsound ? false : true;
    if(game.bgsound){
        game.sound.playBGSound();
        this.blur();
    }
    else {
        game.sound.stopBGSound();
        this.blur();
    }
    });
}

function flashLoadedHandler(){
    game.sound.initSounds();
}
// used for debugging purposes . it adds to the window.status the x,y : where the mouse points on the screen
function mouseHoverProcessor(e){
    //var status = window.status.split(' --- ')[0];
    //window.status=status+' --- '+e.clientX+' '+e.clientY;
}

if(game.debug==1)
  document.onmousemove=mouseHoverProcessor;

/**
 * loadImages
 */
/*
function loadImages() {
    game.images = {};
    game.images.heroStop=Asset.image('img/player-walking-stop.gif');
    game.images.heroWalkingUp=Asset.image('img/player-walking-back.gif');
    game.images.heroWalkingDown=Asset.image('img/player-walking-front.gif');
    game.images.heroWalkingLeft=Asset.image('img/player-walking-left.gif');
    game.images.heroWalkingRight=Asset.image('img/player-walking-right.gif');
} */

